﻿//SSVC.Utils.Page.IncludeFile("../styles/MarkupToolbar.css", "css");

SSVC.Wrap.Dojo.Require([
    "dojo/_base/fx",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/mouse",
    "dojo/on",
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    "dojo/text!scripts/MarkupToolbar.html"],
	function(baseFx, lang, domStyle, mouse, on, _WidgetBase, _TemplatedMixin, template)
	{
		function createSettersAndGetters(self)
		{
			if (self.hasOwnProperty("selectedMarkupType"))
				return;

			Object.defineProperty(self, "selectedMarkupType",
			{
				get: function()
				{
					return self._selectedMarkupType;
				},
				set: function(value)
				{
					self._selectedMarkupType = value;
				}

			});

			Object.defineProperty(self, "lineType",
			{
				get: function() { return self._selectedLineType; }
			});

			Object.defineProperty(self, "boxType",
			{
				get: function() { return self._selectedBoxType; }
			});

			Object.defineProperty(self, "ellipseType",
			{
				get: function() { return self._selectedEllipseType; }
			});

			Object.defineProperty(self, "lineStyle",
			{
				get: function() { return self._selectedLineStyleType; }
			});

			Object.defineProperty(self, "highlightColor",
			{
				get: function() { return self._highlightColor; },
				set: function(value)
				{
					self._highlightColor = value;

					//Set color of highlight color box
					self.HighlightMarkupColorPicker.domNode.style.backgroundColor = value;
				}
			});

			Object.defineProperty(self, "lineColor",
			{
				get: function() { return self._lineColor; },
				set: function(value)
				{
					self._lineColor = value;

					//Set color of line color box
					self.LineMarkupColorPicker.domNode.style.backgroundColor = value;
				}
			});

			Object.defineProperty(self, "textColor",
			{
				get: function() { return self._textColor; },
				set: function(value)
				{
					self._textColor = value;

					//Set color of line color box
					self.TextMarkupColorPicker.domNode.style.backgroundColor = value;
				}
			});

			Object.defineProperty(self, "boxColor",
			{
				get: function() { return self._boxColor; },
				set: function(value)
				{
					self._boxColor = value;

					//Set color of box color box
					self.BoxMarkupColorPicker.domNode.style.backgroundColor = value;
				}
			});

			Object.defineProperty(self, "ellipseColor",
			{
				get: function() { return self._ellipseColor; },
				set: function(value)
				{
					self._ellipseColor = value;

					//Set color of ellipse color box
					self.EllipseMarkupColorPicker.domNode.style.backgroundColor = value;
				}
			});
		}

		return SSVC.Wrap.Dojo.SetObject("SSVC.MarkupToolbar", SSVC.Wrap.Dojo.Declare([_WidgetBase, _TemplatedMixin],
		{
			templateString: template,
			//"Highlight", "Text", "Line", "Box", "Ellipse", "Select"
			selectedMarkupType: null,
			//"Line", "LineWithArrow"
			lineType: null,
			//"NotFilledBox", "FilledBox"
			boxType: null,
			//"NotFilledEllipse", "FilledEllipse"
			ellipseType: null,
			//"SolidThinLineStyle", "SolidMediumLineStyle", "SolidThickLineStyle", "DashedThinLineStyle", "DashedMediumLineStyle", "DashedThickLineStyle"
			lineStyle: null,
			//Gets or sets the color of highlight
			highlightColor: null,
			//Gets or sets the color of line
			lineColor: null,
			//Gets or sets the color of text
			textColor: null,
			//Gets or sets the color of box
			boxColor: null,
			//Gets or sets the color of ellipse
			ellipseColor: null,

			_selectedMarkupType: null,
			_selectedLineType: null,
			_selectedBoxType: null,
			_selectedEllipseType: null,
			_selectedLineStyleType: null,

			_highlightColor: null,
			_lineColor: null,
			_boxColor: null,
			_textColor: null,
			_ellipseColor: null,
			_colorPicker: null,
			_currentColorBoxId: null,

			HighlightMarkupButton: null,
			HighlightMarkupColorPicker: null,
			TextMarkupButton: null,
			TextMarkupColorPicker: null,
			LineMarkupButton: null,
			LineMarkupSelectButton: null,
			LineMarkupColorPicker: null,
			BoxMarkupButton: null,
			BoxMarkupSelectButton: null,
			BoxMarkupColorPicker: null,
			EllipseMarkupButton: null,
			EllipseMarkupSelectButton: null,
			EllipseMarkupColorPicker: null,
			LineStyleMarkupButton: null,
			LineStyleSelectButton: null,
			SelectMarkupButton: null,
			ClearMarkupButton: null,
			LineMenuItem: null,
			LineWithArrowMenuItem: null,
			NotFilledBoxMenuItem: null,
			FilledBoxMenuItem: null,
			NotFilledEllipseMenuItem: null,
			FilledEllipseMenuItem: null,
			SolidThinLineStyleMenuItem: null,
			SolidMediumLineStyleMenuItem: null,
			SolidThickLineStyleMenuItem: null,
			DashedThinLineStyleMenuItem: null,
			DashedMediumLineStyleMenuItem: null,
			DashedThickLineStyleMenuItem: null,

			_colorPickerWasChangedManually: null,
			_colorPickerIsDisplayed: null,

			constructor: function()
			{
				this.id = "MarkupToolbarContainer";

				this._colorPickerWasChangedManually = false;
				this._colorPickerIsDisplayed = false;

				createSettersAndGetters(this);
			},

			postCreate: function()
			{
				this.HighlightMarkupButton = new SSVC.UI.mtButton("HighlightMarkupButton", ["MarkupButton", "HighlightMarkupButton", "MarkupButtonWithColorPicker"]);
				this.HighlightMarkupButton.placeAt(this.HighlightDiv);
				this.HighlightMarkupButton.OnClick = lang.hitch(this, "_onHighlightButtonClickEventHandler");

				this.HighlightMarkupColorPicker = new SSVC.UI.mtButton("HighlightMarkupColorPicker", ["MarkupColorPicker"]);
				this.HighlightMarkupColorPicker.placeAt(this.HighlightDiv);
				this.HighlightMarkupColorPicker.OnClick = SSVC.Wrap.Dojo.Partial(this._onToolbarColorBoxClickEventHandler, this);

				this.TextMarkupButton = new SSVC.UI.mtButton("TextMarkupButton", ["MarkupButton", "TextMarkupButton", "MarkupButtonWithColorPicker"]);
				this.TextMarkupButton.placeAt(this.TextDiv);
				this.TextMarkupButton.OnClick = lang.hitch(this, "_onTextButtonClickEventHandler");

				this.TextMarkupColorPicker = new SSVC.UI.mtButton("TextMarkupColorPicker", ["MarkupColorPicker"]);
				this.TextMarkupColorPicker.placeAt(this.TextDiv);
				this.TextMarkupColorPicker.OnClick = SSVC.Wrap.Dojo.Partial(this._onToolbarColorBoxClickEventHandler, this);

				this.LineMarkupButton = new SSVC.UI.mtButton("LineMarkupButton", ["MarkupButton", "LineMarkupButton", "LeftMarkupButton"]);
				this.LineMarkupButton.placeAt(this.LineDiv);
				this.LineMarkupButton.OnClick = lang.hitch(this, "_onLineButtonClickEventHandler");

				this.LineMarkupSelectButton = new SSVC.UI.mtButton("LineMarkupSelectButton", ["MarkupButton", "TriangleMarkupToolbar", "RightMarkupButton", "MarkupButtonWithColorPicker"]);
				this.LineMarkupSelectButton.placeAt(this.LineDiv);
				this.LineMarkupSelectButton.OnClick = lang.hitch(this, "_onLineMenuClickEventHandler");

				this.LineMarkupColorPicker = new SSVC.UI.mtButton("LineMarkupColorPicker", ["MarkupColorPicker"]);
				this.LineMarkupColorPicker.placeAt(this.LineDiv);
				this.LineMarkupColorPicker.OnClick = SSVC.Wrap.Dojo.Partial(this._onToolbarColorBoxClickEventHandler, this);

				this.BoxMarkupButton = new SSVC.UI.mtButton("BoxMarkupButton", ["MarkupButton", "NotFilledBoxMarkupButton", "LeftMarkupButton"]);
				this.BoxMarkupButton.placeAt(this.BoxDiv);
				this.BoxMarkupButton.OnClick = lang.hitch(this, "_onBoxButtonClickEventHandler");

				this.BoxMarkupSelectButton = new SSVC.UI.mtButton("BoxMarkupSelectButton", ["MarkupButton", "TriangleMarkupToolbar", "RightMarkupButton", "MarkupButtonWithColorPicker"]);
				this.BoxMarkupSelectButton.placeAt(this.BoxDiv);
				this.BoxMarkupSelectButton.OnClick = lang.hitch(this, "_onBoxMenuClickEventHandler");

				this.BoxMarkupColorPicker = new SSVC.UI.mtButton("BoxMarkupColorPicker", ["MarkupColorPicker"]);
				this.BoxMarkupColorPicker.placeAt(this.BoxDiv);
				this.BoxMarkupColorPicker.OnClick = SSVC.Wrap.Dojo.Partial(this._onToolbarColorBoxClickEventHandler, this);

				this.EllipseMarkupButton = new SSVC.UI.mtButton("EllipseMarkupButton", ["MarkupButton", "NotFilledEllipseMarkupButton", "LeftMarkupButton"]);
				this.EllipseMarkupButton.placeAt(this.EllipseDiv);
				this.EllipseMarkupButton.OnClick = lang.hitch(this, "_onEllipseButtonClickEventHandler");

				this.EllipseMarkupSelectButton = new SSVC.UI.mtButton("EllipseMarkupSelectButton", ["MarkupButton", "TriangleMarkupToolbar", "RightMarkupButton", "MarkupButtonWithColorPicker"]);
				this.EllipseMarkupSelectButton.placeAt(this.EllipseDiv);
				this.EllipseMarkupSelectButton.OnClick = lang.hitch(this, "_onEllipseMenuClickEventHandler");

				this.EllipseMarkupColorPicker = new SSVC.UI.mtButton("EllipseMarkupColorPicker", ["MarkupColorPicker"]);
				this.EllipseMarkupColorPicker.placeAt(this.EllipseDiv);
				this.EllipseMarkupColorPicker.OnClick = SSVC.Wrap.Dojo.Partial(this._onToolbarColorBoxClickEventHandler, this);

				this.LineStyleMarkupButton = new SSVC.UI.mtButton("LineStyleMarkupButton", ["MarkupButton", "SolidMediumMarkupButton", "LeftMarkupButton"]);
				this.LineStyleMarkupButton.placeAt(this.LineStyleDiv);
				this.LineStyleMarkupButton.OnClick = lang.hitch(this, "_onLineStyleButtonClickEventHandler");

				this.LineStyleSelectButton = new SSVC.UI.mtButton("LineStyleSelectButton", ["MarkupButton", "TriangleMarkupToolbar", "RightMarkupButton"]);
				this.LineStyleSelectButton.placeAt(this.LineStyleDiv);
				this.LineStyleSelectButton.OnClick = lang.hitch(this, "_onLineStyleMenuClickEventHandler");

				this.SelectMarkupButton = new SSVC.UI.mtButton("SelectMarkupButton", ["MarkupButton", "SelectMarkupButton"]);
				this.SelectMarkupButton.placeAt(this.SelectDiv);
				this.SelectMarkupButton.OnClick = lang.hitch(this, "_onSelectButtonClickEventHandler");

				this.ClearMarkupButton = new SSVC.UI.mtButton("ClearMarkupButton", ["MarkupButton", "ClearMarkupButton"]);
				this.ClearMarkupButton.placeAt(this.ClearDiv);
				this.ClearMarkupButton.OnClick = lang.hitch(this, "_onClearButtonClickEventHandler");

				this.LineMenuItem = new SSVC.UI.mtButton("LineMenuItem", ["MarkupButton", "LineMarkupButton", "TopMenuItem"]);
				this.LineMenuItem.placeAt(this.LinesMenu);
				this.LineMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineMenuItemClickEventHandler, this);

				this.LinesMenu.appendChild(document.createElement("br"));

				this.LineWithArrowMenuItem = new SSVC.UI.mtButton("LineWithArrowMenuItem", ["MarkupButton", "ArrowMarkupButton", "BottomMenuItem"]);
				this.LineWithArrowMenuItem.placeAt(this.LinesMenu);
				this.LineWithArrowMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineMenuItemClickEventHandler, this);

				this.NotFilledBoxMenuItem = new SSVC.UI.mtButton("NotFilledBoxMenuItem", ["MarkupButton", "NotFilledBoxMarkupButton", "TopMenuItem"]);
				this.NotFilledBoxMenuItem.placeAt(this.BoxesMenu);
				this.NotFilledBoxMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onBoxMenuItemClickEventHandler, this);

				this.BoxesMenu.appendChild(document.createElement("br"));

				this.FilledBoxMenuItem = new SSVC.UI.mtButton("FilledBoxMenuItem", ["MarkupButton", "FilledBoxMarkupButton", "BottomMenuItem"]);
				this.FilledBoxMenuItem.placeAt(this.BoxesMenu);
				this.FilledBoxMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onBoxMenuItemClickEventHandler, this);

				this.NotFilledEllipseMenuItem = new SSVC.UI.mtButton("NotFilledEllipseMenuItem", ["MarkupButton", "NotFilledEllipseMarkupButton", "TopMenuItem"]);
				this.NotFilledEllipseMenuItem.placeAt(this.EllipsesMenu);
				this.NotFilledEllipseMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onEllipseMenuItemClickEventHandler, this);

				this.EllipsesMenu.appendChild(document.createElement("br"));

				this.FilledEllipseMenuItem = new SSVC.UI.mtButton("FilledEllipseMenuItem", ["MarkupButton", "FilledEllipseMarkupButton", "BottomMenuItem"]);
				this.FilledEllipseMenuItem.placeAt(this.EllipsesMenu);
				this.FilledEllipseMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onEllipseMenuItemClickEventHandler, this);

				this.SolidThinLineStyleMenuItem = new SSVC.UI.mtButton("SolidThinLineStyleMenuItem", ["MarkupButton", "SolidThinMarkupButton", "MiddleMenuItem"]);
				this.SolidThinLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.SolidThinLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				this.LineStylesMenu.appendChild(document.createElement("br"));

				this.SolidMediumLineStyleMenuItem = new SSVC.UI.mtButton("SolidMediumLineStyleMenuItem", ["MarkupButton", "SolidMediumMarkupButton", "TopMenuItem"]);
				this.SolidMediumLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.SolidMediumLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				this.LineStylesMenu.appendChild(document.createElement("br"));

				this.SolidThickLineStyleMenuItem = new SSVC.UI.mtButton("SolidThickLineStyleMenuItem", ["MarkupButton", "SolidThickMarkupButton", "MiddleMenuItem"]);
				this.SolidThickLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.SolidThickLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				this.LineStylesMenu.appendChild(document.createElement("br"));

				this.DashedThinLineStyleMenuItem = new SSVC.UI.mtButton("DashedThinLineStyleMenuItem", ["MarkupButton", "DashedThinMarkupButton", "BottomMenuItem"]);
				this.DashedThinLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.DashedThinLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				this.LineStylesMenu.appendChild(document.createElement("br"));

				this.DashedMediumLineStyleMenuItem = new SSVC.UI.mtButton("DashedMediumLineStyleMenuItem", ["MarkupButton", "DashedMediumMarkupButton", "MiddleMenuItem"]);
				this.DashedMediumLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.DashedMediumLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				this.LineStylesMenu.appendChild(document.createElement("br"));

				this.DashedThickLineStyleMenuItem = new SSVC.UI.mtButton("DashedThickLineStyleMenuItem", ["MarkupButton", "DashedThickMarkupButton", "MiddleMenuItem"]);
				this.DashedThickLineStyleMenuItem.placeAt(this.LineStylesMenu);
				this.DashedThickLineStyleMenuItem.OnClick = SSVC.Wrap.Dojo.Partial(this._onLineStyleMenuItemClickEventHandler, this);

				SSVC.Wrap.Dojo.RequirePath("dijit.popup");
				SSVC.Wrap.Dojo.Require(["dijit/TooltipDialog", "dijit/ColorPalette"], SSVC.Wrap.Dojo.Hitch(this, function(TooltipDialog, ColorPalette)
				{
					//Create color picker tooltip dialog
					this.ColorPickerContainer = new TooltipDialog({}, this.ColorPickerContainer);
					this.ColorPickerContainer.startup();
					this._openColorPicker(null);
					this._closeColorPicker();

					//Create new instance of color picker
					this._colorPicker = new ColorPalette({}, this.ColorPicker);
					this._colorPicker.startup();
					this._colorPicker.on("change", SSVC.Wrap.Dojo.Partial(this._onColorPickerChangeEventHandler, this));
				}));

				this.highlightColor = "#ffff00";
				this.lineColor = "#ff0000";
				this.boxColor = "#0000ff";
				this.textColor = "#000000";
				this.ellipseColor = "#32cd32";
			},

			//-----------------------------------------------------------------------------------------------
			getStrokeWidth: function()
			{
				var lineWidth = 1;

				switch (this.lineStyle)
				{
					case "SolidThinLineStyle":
						lineWidth = 1;
						break;
					case "SolidMediumLineStyle":
						lineWidth = 2;
						break;
					case "SolidThickLineStyle":
						lineWidth = 3;
						break;
					case "DashedThinLineStyle":
						lineWidth = 1;
						break;
					case "DashedMediumLineStyle":
						lineWidth = 2;
						break;
					case "DashedThickLineStyle":
						lineWidth = 3;
						break;
				}

				return lineWidth;
			},

			getStrokeDash: function()
			{
				var lineDash = "none";

				switch (this.lineStyle)
				{
					case "SolidThinLineStyle":
						lineDash = "none";
						break;
					case "SolidMediumLineStyle":
						lineDash = "none";
						break;
					case "SolidThickLineStyle":
						lineDash = "none";
						break;
					case "DashedThinLineStyle":
						lineDash = "5";
						break;
					case "DashedMediumLineStyle":
						lineDash = "5";
						break;
					case "DashedThickLineStyle":
						lineDash = "5";
						break;
				}

				return lineDash;
			},

			//-----------------------------------------------------------------------------------------------
			//"Highlight" button
			OnClickHighlightButton: function() { },
			_onHighlightButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.HighlightMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Highlight";

				this.HighlightMarkupButton.SetPressedState(!pressedState);
				this.OnClickHighlightButton();
			},

			//-----------------------------------------------------------------------------------------------
			//"Text" button
			OnClickTextButton: function() { },
			_onTextButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.TextMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Text";

				this.TextMarkupButton.SetPressedState(!pressedState);
				this.OnClickTextButton();
			},

			//-----------------------------------------------------------------------------------------------
			//"Line" button
			OnClickLineButton: function() { },
			_onLineButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.LineMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Line";
				if (this._selectedLineType == null)
					this._selectedLineType = "Line";

				this.LineMarkupButton.SetPressedState(!pressedState);
				this.OnClickLineButton();
			},

			_onLineMenuClickEventHandler: function()
			{
				this._toggleMenuState(this.LinesMenu);
			},

			_onLineMenuItemClickEventHandler: function(self)
			{
				var lineId = this.domNode.id;

				switch (lineId)
				{
					case "LineMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineMarkupButton, "ArrowMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineMarkupButton, "LineMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineMenuItem, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.LineWithArrowMenuItem, "hidden");

						self._selectedLineType = "Line";
						break;
					case "LineWithArrowMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineMarkupButton, "LineMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineMarkupButton, "ArrowMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineWithArrowMenuItem, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.LineMenuItem, "hidden");

						self._selectedLineType = "LineWithArrow";
						break;
				}

				self._toggleMenuState(self.LinesMenu);

				self.selectedMarkupType = "Line";
			},

			//-----------------------------------------------------------------------------------------------
			//"Box" button
			OnClickBoxButton: function() { },
			_onBoxButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.BoxMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Box";
				if (this._selectedBoxType == null)
					this._selectedBoxType = "NotFilledBox";

				this.BoxMarkupButton.SetPressedState(!pressedState);
				this.OnClickBoxButton();
			},

			_onBoxMenuClickEventHandler: function()
			{
				this._toggleMenuState(this.BoxesMenu);
			},

			_onBoxMenuItemClickEventHandler: function(self)
			{
				var boxId = this.domNode.id;

				switch (boxId)
				{
					case "NotFilledBoxMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.BoxMarkupButton.domNode, "FilledBoxMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.BoxMarkupButton.domNode, "NotFilledBoxMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.NotFilledBoxMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.FilledBoxMenuItem.domNode, "hidden");

						self._selectedBoxType = "NotFilledBox";
						break;
					case "FilledBoxMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.BoxMarkupButton.domNode, "NotFilledBoxMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.BoxMarkupButton.domNode, "FilledBoxMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.FilledBoxMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.NotFilledBoxMenuItem.domNode, "hidden");

						self._selectedBoxType = "FilledBox";
						break;
				}

				self._toggleMenuState(self.BoxesMenu);

				self.selectedMarkupType = "Box";
			},

			//-----------------------------------------------------------------------------------------------
			//"Ellipse" button
			OnClickEllipseButton: function() { },
			_onEllipseButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.EllipseMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Ellipse";
				if (this._selectedEllipseType == null)
					this._selectedEllipseType = "NotFilledEllipse";

				this.EllipseMarkupButton.SetPressedState(!pressedState);
				this.OnClickEllipseButton();
			},

			_onEllipseMenuClickEventHandler: function()
			{
				this._toggleMenuState(this.EllipsesMenu);
			},

			_onEllipseMenuItemClickEventHandler: function(self)
			{
				var ellipseId = this.domNode.id;

				switch (ellipseId)
				{
					case "NotFilledEllipseMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.EllipseMarkupButton.domNode, "FilledEllipseMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.EllipseMarkupButton.domNode, "NotFilledEllipseMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.NotFilledEllipseMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.FilledEllipseMenuItem.domNode, "hidden");

						self._selectedEllipseType = "NotFilledEllipse";
						break;
					case "FilledEllipseMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.EllipseMarkupButton.domNode, "NotFilledEllipseMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.EllipseMarkupButton.domNode, "FilledEllipseMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.FilledEllipseMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.NotFilledEllipseMenuItem.domNode, "hidden");

						self._selectedEllipseType = "FilledEllipse";
						break;
				}

				self._toggleMenuState(self.EllipsesMenu);

				self.selectedMarkupType = "Ellipse";
			},

			//-----------------------------------------------------------------------------------------------
			//"Line style" button
			OnClickLineStyleButton: function() { },
			_onLineStyleButtonClickEventHandler: function()
			{
				this._onLineStyleMenuClickEventHandler(this);
				this.OnClickLineStyleButton();
			},

			_onLineStyleMenuClickEventHandler: function()
			{
				this._toggleMenuState(this.LineStylesMenu);
			},

			_onLineStyleMenuItemClickEventHandler: function(self)
			{
				var lineStyleId = this.domNode.id;

				switch (lineStyleId)
				{
					case "SolidThinLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "SolidThinLineStyle";
						break;
					case "SolidMediumLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "SolidMediumMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "SolidMediumLineStyle";
						break;
					case "SolidThickLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "SolidThickLineStyle";
						break;
					case "DashedThinLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "DashedThinLineStyle";
						break;
					case "DashedMediumLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidMediumMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "DashedMediumLineStyle";
						break;
					case "DashedThickLineStyleMenuItem":
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "SolidThickMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedThinMarkupButton");
						SSVC.Wrap.Dojo.RemoveClass(self.LineStyleMarkupButton.domNode, "DashedMediumMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.LineStyleMarkupButton.domNode, "DashedThickMarkupButton");
						SSVC.Wrap.Dojo.AddClass(self.DashedThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidMediumLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.SolidThickLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedThinLineStyleMenuItem.domNode, "hidden");
						SSVC.Wrap.Dojo.RemoveClass(self.DashedMediumLineStyleMenuItem.domNode, "hidden");

						self._selectedLineStyleType = "DashedThickLineStyle";
						break;
				}

				self._toggleMenuState(self.LineStylesMenu);
			},

			//-----------------------------------------------------------------------------------------------
			//"Select" button
			OnClickSelectButton: function() { },
			_onSelectButtonClickEventHandler: function()
			{
				this.closeMenus();

				var pressedState = this.SelectMarkupButton.IsPressed;
				this.resetAllButtonsState();

				this.selectedMarkupType = pressedState ? "" : "Select";

				this.SelectMarkupButton.SetPressedState(!pressedState);
				this.OnClickSelectButton();
			},

			//-----------------------------------------------------------------------------------------------
			//"Clear/Unfreeze" button
			OnClickClearButton: function() { },
			_onClearButtonClickEventHandler: function()
			{
				this.OnClickClearButton();
			},

			//-----------------------------------------------------------------------------------------------
			//"ColorPicker"

			_toggleStateOfColorPicker: function(node)
			{
				if (this._colorPickerIsDisplayed)
				{
					dijit.popup.close(this.ColorPickerContainer);
					this._colorPickerIsDisplayed = false;
				}
				else
				{
					this.closeMenus();
					dijit.popup.open({ popup: this.ColorPickerContainer, around: node });
					this._colorPickerIsDisplayed = true;
				}
			},

			_openColorPicker: function(node)
			{
				if (!this._colorPickerIsDisplayed)
				{
					this.closeMenus();
					dijit.popup.open({ popup: this.ColorPickerContainer, around: node });
				}
				else
				{
					dijit.popup.close(this.ColorPickerContainer);
					dijit.popup.open({ popup: this.ColorPickerContainer, around: node });
				}

				this._colorPickerIsDisplayed = true;
			},

			_closeColorPicker: function()
			{
				if (this._colorPickerIsDisplayed)
				{
					dijit.popup.close(this.ColorPickerContainer);
				}
				this._colorPickerIsDisplayed = false;
			},

			_onColorPickerChangeEventHandler: function(self, colorValue)
			{
				if (self._colorPickerWasChangedManually)
				{
					return;
				}

				switch (self._currentColorBoxId)
				{
					case "HighlightMarkupColorPicker":
						self.highlightColor = colorValue;
						break;
					case "TextMarkupColorPicker":
						self.textColor = colorValue;
						break;
					case "LineMarkupColorPicker":
						self.lineColor = colorValue;
						break;
					case "BoxMarkupColorPicker":
						self.boxColor = colorValue;
						break;
					case "EllipseMarkupColorPicker":
						self.ellipseColor = colorValue;
						break;
				}

				self._closeColorPicker();
			},

			//-----------------------------------------------------------------------------------------------
			//"ColorBox"

			_onToolbarColorBoxClickEventHandler: function(self)
			{
				var colorBoxId = this.domNode.id;
				var colorBoxWasChanged = !(self._currentColorBoxId == colorBoxId);
				self._currentColorBoxId = colorBoxId;

				self._colorPickerWasChangedManually = true;

				switch (colorBoxId)
				{
					case "HighlightMarkupColorPicker":
						self._colorPicker.set("value", self.highlightColor);
						break;
					case "TextMarkupColorPicker":
						self._colorPicker.set("value", self.textColor);
						break;
					case "LineMarkupColorPicker":
						self._colorPicker.set("value", self.lineColor);
						break;
					case "BoxMarkupColorPicker":
						self._colorPicker.set("value", self.boxColor);
						break;
					case "EllipseMarkupColorPicker":
						self._colorPicker.set("value", self.ellipseColor);
						break;
				}

				self._colorPickerWasChangedManually = false;

				if (colorBoxWasChanged)
				{
					self._openColorPicker(this.domNode);
				}
				else
				{
					self._toggleStateOfColorPicker(this.domNode);
				}
			},

			getSelectedMarkupColor: function()
			{
				var selectedMarkupColor = null;

				switch (this._selectedMarkupType)
				{
					case "Highlight":
						selectedMarkupColor = this._highlightColor;
						break;
					case "Line":
						selectedMarkupColor = this._lineColor;
						break;
					case "Text":
						selectedMarkupColor = this._textColor;
						break;
					case "Box":
						selectedMarkupColor = this._boxColor;
						break;
					case "Ellipse":
						selectedMarkupColor = this._ellipseColor;
						break;
				}

				return selectedMarkupColor;
			},

			//-----------------------------------------------------------------------------------------------

			_toggleMenuState: function(menu)
			{
				var btn = null;

				//Hide other menus
				this._closeColorPicker();

				if (menu != this.LinesMenu)
				{
					SSVC.Wrap.Dojo.AddClass(this.LinesMenu, "hidden");
				}
				else
				{
					btn = this.LineMarkupButton.domNode;
				}

				if (menu != this.BoxesMenu)
				{
					SSVC.Wrap.Dojo.AddClass(this.BoxesMenu, "hidden");
				}
				else
				{
					btn = this.BoxMarkupButton.domNode;
				}

				if (menu != this.EllipsesMenu)
				{
					SSVC.Wrap.Dojo.AddClass(this.EllipsesMenu, "hidden");
				}
				else
				{
					btn = this.EllipseMarkupButton.domNode;
				}

				if (menu != this.LineStylesMenu)
				{
					SSVC.Wrap.Dojo.AddClass(this.LineStylesMenu, "hidden");
				}
				else
				{
					btn = this.LineStyleMarkupButton.domNode;
				}

				//Toggle specified menu
				var rect = btn.getBoundingClientRect();
				menu.style.top = (rect.top + rect.height) + "px";
				menu.style.left = rect.left + "px";
				SSVC.Wrap.Dojo.ToggleClass(menu, "hidden");
			},

			//Hides all menus
			closeMenus: function()
			{
				SSVC.Wrap.Dojo.AddClass(this.LinesMenu, "hidden");
				SSVC.Wrap.Dojo.AddClass(this.BoxesMenu, "hidden");
				SSVC.Wrap.Dojo.AddClass(this.EllipsesMenu, "hidden");
				SSVC.Wrap.Dojo.AddClass(this.LineStylesMenu, "hidden");

				this._closeColorPicker();
			},

			resetAllButtonsState: function()
			{
				this.HighlightMarkupButton.SetPressedState(false);
				this.TextMarkupButton.SetPressedState(false);
				this.LineMarkupButton.SetPressedState(false);
				this.BoxMarkupButton.SetPressedState(false);
				this.EllipseMarkupButton.SetPressedState(false);
				this.SelectMarkupButton.SetPressedState(false);
			}
		}));
	});